﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qltscd.Models;
using qltscd.Models.ScreenClient;
using qltscd.Contansts;
using Telerik.Web.Mvc;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using qltscd.Helper;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.Routing;

namespace qltscd.Controllers
{
    public class SuaChuaController : Controller
    {
        //
        // GET: /SuaChua/

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (SessionHelper.QuyenNhom[qltscd.Contansts.KeyContansts.QuyenTacVu] == '0')
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                {
                    controller = "KhongCoQuyen",
                    action = "Index"
                }));
            }
        }

        private QLTBEntities db = new QLTBEntities();

        //public ActionResult Index()
        //{
        //    return View();
        //}

        public ActionResult Index()
        {
            Models.BaseClient.PhieuSuaChua suachua = new Models.BaseClient.PhieuSuaChua() { IdentityThietBi = -1};
            ViewBag.SerialThietBi = new SelectList(db.DanhMucThietBis.OrderBy(i => i.SerialThietBi), "Id5entityThietBi", "SerialThietBi", suachua.IdentityThietBi);
            return View(suachua);
        }

        [HttpPost]
        public ActionResult Index(Models.BaseClient.PhieuSuaChua phieusuachua)
        {
            PhieuSuaChua psc = new PhieuSuaChua();
            if (ModelState.IsValid)
            {
                if (phieusuachua.IdentityPhieuSuaChua == -1)
                {
                   
                    if (phieusuachua.MaPhieuSuaChua != psc.MaPhieuSuaChua)
                    {
                        if (db.PhieuSuaChuas.Count(o => o.MaPhieuSuaChua == phieusuachua.MaPhieuSuaChua) > 0)
                        {
                            ViewData[SessionConstants.ERROR_MESSAGE] = "Mã phiếu sữa chữa " + phieusuachua.MaPhieuSuaChua + " đã tồn tại";
                            return View(phieusuachua);
                        }
                    }
                    ObjectHelper.Copy(phieusuachua, psc);
                    db.PhieuSuaChuas.AddObject(psc);
                }
                //else
                //{
                //    nsx = db.phieusuachuas.Single(i => i.IdentityNuocSanXuat == phieusuachua.IdentityNuocSanXuat);
                //    if (phieusuachua.MaNuocSanXuat != nsx.MaNuocSanXuat)
                //    {
                //        if (db.phieusuachuas.Count(o => o.MaNuocSanXuat == phieusuachua.MaNuocSanXuat) > 0)
                //        {
                //            ViewData[SessionConstants.ERROR_MESSAGE] = "Mã nước sản xuất " + phieusuachua.MaNuocSanXuat + " đã tồn tại";
                //            return View(phieusuachua);
                //        }
                //    }
                //    ObjectHelper.Copy(phieusuachua, nsx);
                //    db.ObjectStateManager.ChangeObjectState(nsx, EntityState.Modified);
                //}
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(phieusuachua);
        }
    }
}
